package org.wjk.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class ObjSerializeUtils {
    public static byte [] serialize(Object obj)
    {
        try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos)){
            oos.writeObject(obj);
            return bos.toByteArray();
        }
        catch (IOException e)
        {
            log.error("序列化对象时发生错误，具体信息为：{}", e.getMessage());
            return null;
        }
    }
    public static Object deserialize(byte[] source)
    {
        try(ByteArrayInputStream bis = new ByteArrayInputStream(source); ObjectInputStream ois = new ObjectInputStream(bis))
        {
            return ois.readObject();
        }
        catch (Exception e)
        {
            log.error("反序列化对象时发生错误，具体信息为：{}", e.getMessage());
            return null;
        }
    }
}
